﻿using Furion;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Mrhd.Core.Extensions;
using System;
using System.Threading.Tasks;

namespace Mrhd.Application
{
    /// <summary>
    /// 系统服务接口
    /// </summary>
    public class SystemAppService : IDynamicApiController
    {
        private readonly ISystemService _systemService;
        private readonly IMemoryCache _cache;
        private readonly IDistributedCache _distributedCache;
        private readonly ILogger<SystemAppService> _logger;
        public SystemAppService(ISystemService systemService, IMemoryCache cache, IDistributedCache distributedCache, ILogger<SystemAppService> logger)
        {
            _systemService = systemService;
            _cache = cache;
            _distributedCache = distributedCache;
            _logger = logger;
        }

        /// <summary>
        /// 获取系统描述
        /// </summary>
        /// <returns></returns>
        public string GetDescription()
        {
            return _systemService.GetDescription();
        }

        /// <summary>
        /// 测试mapster数据转化
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public SystemEntityDTO PostInfo(SystemEntity entity)
        {
            return entity.Adapt<SystemEntityDTO>();
        }

        /// <summary>
        /// 测试mapster数据转化
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public SystemEntity PostInfo2(SystemEntityDTO entity)
        {
            return entity.Adapt<SystemEntity>();
        }

        /// <summary>
        /// 测试内存缓存
        /// </summary>
        /// <returns></returns>
        public async Task<DateTime> GetTime()
        {
            var now = await _cache.GetOrCreateAsync("CurrentTime", entry =>
            {
                entry.AbsoluteExpirationRelativeToNow = new TimeSpan(0, 1, 0);
                return Task.FromResult(DateTime.Now);
            });
            return now;
        }

        /// <summary>
        /// 测试分布式缓存（存）
        /// </summary>
        /// <returns></returns>
        public async Task<string> SetCache()
        {
            await _distributedCache.ObjectSetAsync("user:1", new { id = 1, name = "孙小双", age = 22 });
            return "成功";
        }

        /// <summary>
        /// 测试分布式缓存（取）
        /// </summary>
        /// <returns></returns>
        public async Task<dynamic> GetCache()
        {
            var obj = await _distributedCache.ObjectGetAsync<dynamic>("user:1");
            return obj;
        }

        /// <summary>
        /// 测试日志记录
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public string GetLog(string msg)
        {
            _logger.LogDebug(msg);
            return msg;
        }

        /// <summary>
        /// 测试获取配置选项
        /// </summary>
        /// <returns></returns>
        public AppInfoOptions GetOptions()
        {
            return App.GetOptions<AppInfoOptions>();
        }

    }

}
